import doctorService from '../../services/doctor'
import * as types from '../mutation-types'
// initial state
const state = {
  cityData: [],
  departments:[],
  titles:[]
}

// getters
const getters = {

}

// actions
const actions = {
  loadCity({commit}) {
    return new Promise((resolve,reject) =>{
      doctorService.loadCitiesList((data) =>{
        commit(types.LOAD_CITY,data);
        resolve(data);
      });
    })
  },
  loadDepartments({commit}) {
    return new Promise((resolve,reject) =>{
      doctorService.loadDoctorDepartmentList((data) =>{
        commit(types.LOAD_DEPARTMENT,data);
        resolve(data);
      });
    })
  },
  loadTitles({commit}) {
    return new Promise((resolve,reject) =>{
      doctorService.loadDoctorTitleList((data) =>{
        commit(types.LOAD_TITLES,data);
        resolve(data);
      });
    })
  },
}

// mutations
const mutations = {
  [types.LOAD_CITY] (state, data) {
    state.cityData = [{id:'',name:'全部'}];
    for(let i=0;i<data.data.length;i++) {
      state.cityData.push(data.data[i]);
    }
  },
  [types.LOAD_DEPARTMENT] (state, data) {
    data.data.forEach(item=>{
      state.departments.push({
        name:item.name,
        value:item.id
      })
    });
  },
  [types.LOAD_TITLES] (state, data) {
    data.data.forEach(item=>{
      state.titles.push({
        name:item.name,
        value:item.id
      })
    });
  },
}

export default {
  state,
  getters,
  actions,
  mutations
}
